package cn.doitedu.javase.calculate_engine.server;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

/**
 * 处理 /favicon.ico 图片请求
 */
public class FaviconHandler implements RequestHandler{

    @Override
    public byte[] handlerRequest(UrlAndParams urlAndParams) throws IOException {

        String responseHeader =
                "HTTP/1.1 200 OK\n" +  // 响应行
                        "Accept-Ranges:bytes\n" +  // 响应头
                        "Content-Type:image/x-icon\n" + // 响应头
                        "\n";

        byte[] headerBytes = responseHeader.getBytes("UTF-8");
        byte[] bodyBytes = FileUtils.readFileToByteArray(new File("html/favicon.ico"));

        byte[] wholeBytes = new byte[headerBytes.length + bodyBytes.length];

        System.arraycopy(headerBytes,0,wholeBytes,0,headerBytes.length);
        System.arraycopy(bodyBytes,0,wholeBytes,headerBytes.length,bodyBytes.length);

        return wholeBytes;
    }
}
